﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace BF
{
    public static class AESUtil
    {
        private static readonly AES aes;

        static AESUtil()
        {
            AESSetting setting = AESSetting.GetInstance();
            string key = HashUtil.GetMD5Hash(setting.PREFIX + setting.KEY).Substring(0, 16).ToLower();
            string iv = HashUtil.GetMD5Hash(setting.PREFIX + setting.IV).Substring(0, 16).ToLower();
            aes = new AES(key, iv, CipherMode.CBC, PaddingMode.PKCS7);
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="toEncrypt">需要加密的字符串</param>
        /// <returns>加密后的base64字符串</returns>
        public static string Encrypt(string toEncrypt)
        {
            return aes.Encrypt(toEncrypt);
        }

        /// <summary>
        /// 加密
        /// </summary>
        public static byte[] Encrypt(byte[] toEncrypt)
        {
            return aes.Encrypt(toEncrypt);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="base64StrToDecrypt">需要解密的base64字符串</param>
        public static string Decrypt(string base64StrToDecrypt)
        {
            return aes.Decrypt(base64StrToDecrypt);
        }

        /// <summary>
        /// 解密
        /// </summary>
        public static byte[] Decrypt(byte[] toDecrypt)
        {
            return aes.Decrypt(toDecrypt);
        }
    }
}